package com.rds.framework.web.service;

import java.util.*;

import com.rds.common.core.domain.entity.RdaPowerControl;
import com.rds.common.core.redis.RedisCache;
import com.rds.common.utils.spring.SpringUtils;
import com.rds.system.service.IRdaPowerControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.rds.common.core.domain.entity.SysRole;
import com.rds.common.core.domain.entity.SysUser;
import com.rds.system.service.ISysMenuService;
import com.rds.system.service.ISysRoleService;

/**
 * 用户权限处理
 * 
 * @author rds
 */
@Component
public class SysPermissionService
{
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private IRdaPowerControlService powerControlService;

    /**
     * 获取角色数据权限
     * 
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user)
    {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     * 
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user)
    {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            perms.add("*:*:*");
        }
        else
        {
            List<SysRole> roles = user.getRoles();
            if (!roles.isEmpty() && roles.size() > 1)
            {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles)
                {
                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            }
            else
            {
                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
            }
        }
        return perms;
    }

    /**
     * 获取用户角色
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getUserRole(SysUser user)
    {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            return perms;
        }
        else
        {
            perms.addAll(roleService.selectRoleKeys(user.getUserId()).get("roleSet"));
            if(perms.size()>0){
                //获取角色的数据权限
                List<RdaPowerControl> powerControls=powerControlService.selectPrivilegeId(perms);
                Map<String,Set<Long>> userPermission = new HashMap<>();
                List<String> readList = new ArrayList<>();
//                Set<String[]> readList=new HashSet<>();
                Set<Long> readset=new HashSet<>();
                Set<Long> modifyset=new HashSet<>();
                Set<Long> removeset=new HashSet<>();
                Set<Long> createdset=new HashSet<>();
                Set<Long> releaseset=new HashSet<>();
                Set<Long> reviseset=new HashSet<>();
                Set<Long> obsoleteset=new HashSet<>();
                Set<Long> viewsset=new HashSet<>();
                Set<Long> checkinset=new HashSet<>();
                Set<Long> downloadset=new HashSet<>();
                Set<Long> promoteset=new HashSet<>();
                Set<Long> demoteset=new HashSet<>();
                Set<Long> subordinateset=new HashSet<>();
                String read="";
                String modify="";
                String remove="";
                String created="";
                String release="";
                String revise="";
                String obsolete="";
                String views="";
                String checkin="";
                String download="";
                String promote="";
                String demote="";
                String subordinate="";
                for (RdaPowerControl privilege:powerControls){
                    if(privilege.getRead()){
                        readset.add(privilege.getPolicyStateId());
                        readList.add(privilege.getPolicyStateId()+";"+privilege.getAttribute()+";"+privilege.getAttributeValue());
                        if(read.length()>0){
                            read = read +","+ privilege.getPolicyStateId();
                        }else{
                            read = read + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getModify()){
                        modifyset.add(privilege.getPolicyStateId());
                        if(modify.length()>0){
                            modify= modify +","+ privilege.getPolicyStateId();
                        }else{
                            modify= modify + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getRemove()){
                        removeset.add(privilege.getPolicyStateId());
                        if(remove.length()>0){
                            remove= remove +","+ privilege.getPolicyStateId();
                        }else{
                            remove= remove + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getCreated()){
                        createdset.add(privilege.getPolicyStateId());
                        if(created.length()>0){
                            created= created +","+ privilege.getPolicyStateId();
                        }else{
                            created= created + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getRelease()){
                        releaseset.add(privilege.getPolicyStateId());
                        if(release.length()>0){
                            release= release +","+ privilege.getPolicyStateId();
                        }else{
                            release= release + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getRevise()){
                        reviseset.add(privilege.getPolicyStateId());
                        if(revise.length()>0){
                            revise= revise +","+ privilege.getPolicyStateId();
                        }else{
                            revise= revise + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getObsolete()){
                        obsoleteset.add(privilege.getPolicyStateId());
                        if(obsolete.length()>0){
                            obsolete= obsolete +","+ privilege.getPolicyStateId();
                        }else{
                            obsolete= obsolete + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getViews()){
                        viewsset.add(privilege.getPolicyStateId());
                        if(views.length()>0){
                            views= views +","+ privilege.getPolicyStateId();
                        }else{
                            views= views + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getCheckIn()){
                        checkinset.add(privilege.getPolicyStateId());
                        if(checkin.length()>0){
                            checkin= checkin +","+ privilege.getPolicyStateId();
                        }else{
                            checkin= checkin + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getDownload()){
                        downloadset.add(privilege.getPolicyStateId());
                        if(download.length()>0){
                            download= download +","+ privilege.getPolicyStateId();
                        }else{
                            download= download + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getPromote()){
                        promoteset.add(privilege.getPolicyStateId());
                        if(promote.length()>0){
                            promote= promote +","+ privilege.getPolicyStateId();
                        }else{
                            promote= promote + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getDemote()){
                        demoteset.add(privilege.getPolicyStateId());
                        if(demote.length()>0){
                            demote= demote +","+ privilege.getPolicyStateId();
                        }else{
                            demote= demote + privilege.getPolicyStateId();
                        }
                    }
                    if(privilege.getSubordinate()){
                        subordinateset.add(privilege.getPolicyStateId());
                        if(subordinate.length()>0){
                            subordinate= subordinate +","+ privilege.getPolicyStateId();
                        }else{
                            subordinate= subordinate + privilege.getPolicyStateId();
                        }
                    }
                }
                //缓存角色的数据权限
                userPermission.put("read",readset);
                userPermission.put("modify",readset);
                userPermission.put("remove",readset);
                userPermission.put("created",readset);
                userPermission.put("release",readset);
                userPermission.put("revise",readset);
                userPermission.put("obsolete",readset);
                userPermission.put("views",readset);
                userPermission.put("checkin",readset);
                userPermission.put("download",readset);
                userPermission.put("promote",readset);
                userPermission.put("demote",readset);
                userPermission.put("subordinate",readset);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-read", read);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-modify", modify);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-remove", remove);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-created", created);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-release", release);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-revise", revise);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-obsolete", obsolete);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-views", views);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-checkin", checkin);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-download", download);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-promote", promote);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-demote", demote);
                SpringUtils.getBean(RedisCache.class).setCacheObject(user.getUserId()+"-subordinate", subordinate);

                System.out.println("====================================="+readList);
                if(readList.size()>0){
                    SpringUtils.getBean(RedisCache.class).deleteObject(user.getUserId()+"-readList");
                    SpringUtils.getBean(RedisCache.class).setCacheList(user.getUserId()+"-readList", readList);
                }

                SpringUtils.getBean(RedisCache.class).setCacheMap(user.getUserId()+"userPermission", userPermission);
                System.out.println("=====================================缓存登录用户的数据权限");
            }
        }


        return perms;
    }
}
